<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DeepSeek：spring-ai测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .chat-container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
        }
        #chat-display {
            height: 400px;
            overflow-y: auto;
            margin-bottom: 15px;
            border: 1px solid #e0e0e0;
            border-radius: 4px;
            padding: 10px;
        }
        .message {
            margin-bottom: 10px;
            padding: 8px 12px;
            border-radius: 4px;
            max-width: 80%;
        }
        .user-message {
            background-color: #e3f2fd;
            margin-left: auto;
        }
        .bot-message {
            background-color: #f1f1f1;
            margin-right: auto;
        }
        #input-container {
            display: flex;
            gap: 10px;
        }
        #message-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        #send-button {
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        #send-button:hover {
            background-color: #0056b3;
        }
        .loading {
            color: #666;
            font-style: italic;
        }
    </style>
</head>
<body>
<div class="chat-container">
    <h2>DeepSeek：spring-ai测试</h2>
    <div id="chat-display"></div>
    <div id="input-container">
        <input type="text" id="message-input" placeholder="输入您的问题...">
        <button id="send-button" onclick="sendMessage()">发送</button>
    </div>
</div>

<script>
    // 添加消息到聊天框
    function addMessage(content, isUser) {
        const chatDisplay = document.getElementById('chat-display');
        const messageDiv = document.createElement('div');
        messageDiv.className = isUser ? 'message user-message' : 'message bot-message';
        messageDiv.textContent = content;
        chatDisplay.appendChild(messageDiv);
        chatDisplay.scrollTop = chatDisplay.scrollHeight;
    }

    // 发送消息到后端接口
    async function sendMessage() {
        const input = document.getElementById('message-input');
        const message = input.value.trim();

        if (!message) {
            alert('请输入内容');
            return;
        }

        // 显示用户消息
        addMessage(message, true);
        input.value = '';

        // 显示加载状态
        const loadingDiv = document.createElement('div');
        loadingDiv.className = 'message bot-message loading';
        loadingDiv.textContent = '思考中...';
        document.getElementById('chat-display').appendChild(loadingDiv);

        try {
            // 调用后端接口
            const response = await fetch(`/deepseek/chat?message=${encodeURIComponent(message)}`);

            if (!response.ok) {
                throw new Error(`HTTP错误 ${response.status}`);
            }

            const answer = await response.text();

            // 移除加载状态
            document.getElementById('chat-display').removeChild(loadingDiv);

            // 显示AI回复
            addMessage(answer, false);
        } catch (error) {
            // 移除加载状态
            document.getElementById('chat-display').removeChild(loadingDiv);

            // 显示错误信息
            addMessage(`请求失败: ${error.message}`, false);
            console.error('请求失败:', error);
        }
    }

    // 支持按Enter键发送
    document.getElementById('message-input').addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            sendMessage();
        }
    });
</script>
</body>
</html>